package com.byzk.p2p.app.controller;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.byzk.p2p.admin.base.controller.BaseController;
import com.byzk.p2p.admin.base.util.JsonMsg;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.score.dto.ScoreRecord;
import com.byzk.p2p.admin.score.service.ScoreRecordService;
import com.byzk.p2p.admin.score.vo.ScoreRecordVo;
import com.byzk.p2p.admin.system.dto.Good;
import com.byzk.p2p.admin.system.dto.GoodBuy;
import com.byzk.p2p.admin.system.service.GoodBuyService;
import com.byzk.p2p.admin.system.service.GoodService;
import com.byzk.p2p.admin.system.vo.GoodBuyVo;
import com.byzk.p2p.admin.system.vo.GoodVo;
import com.byzk.p2p.admin.user.dto.UserAccount;
import com.byzk.p2p.admin.user.service.UserAccountService;

@Controller
@RequestMapping("/app/good")
@Scope("prototype")
public class AppGoodController extends BaseController {

	@Autowired
	private GoodService goodService;
	@Autowired
	private GoodBuyService goodBuyService;
	@Autowired
	private ScoreRecordService scoreRecordService;
	@Autowired
	private UserAccountService accountService;
	
	/**
	 * 商品列表
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public String list() throws Exception{
		GoodVo good =new GoodVo();
		PageInfo<GoodVo> pageInfo = goodService.listGood(good);
		super.printJSON(new JsonMsg(-1, "操作成功", pageInfo.getPageList()));
		return null;
	}
	
	/**
	 * 商品详情
	 * @param gid 商品id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/detail")
	public String detail(@RequestParam("gid") Integer gid) throws Exception{
		GoodVo goodVo = goodService.detail(gid);
		super.printJSON(new JsonMsg(-1, "操作成功", goodVo));
		return null;
	}
	
	/**
	 * 商品购买
	 * @param uid 用户id
	 * @param gid 商品id
	 * @param count 兑换个数
	 * @param tel 收件人手机号
	 * @param address 收件地址
	 * @param name 收件人姓名
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/buy")
	public void buy(@RequestParam("uid") Integer uid,
					  @RequestParam("gid") Integer gid,
					  @RequestParam("count") Integer count,
					  @RequestParam("tel") String tel,
					  @RequestParam("address") String address,
					  @RequestParam("name") String receiver) throws Exception{
		//购买数量问题
		if(count <1){
			super.printJSON(new JsonMsg(1, "商品数量不能小于1！"));
			return;
		}
		if (StringUtils.isBlank(receiver)) {
			super.printJSON(new JsonMsg(2, "收货人姓名不能为空！"));
			return;
		}
		if (StringUtils.isBlank(tel)) {
			super.printJSON(new JsonMsg(3, "电话不能为空！"));
			return;
		}
		if (StringUtils.isBlank(address)) {
			super.printJSON(new JsonMsg(4, "收货地址不能为空！"));
			return;
		}
		
		GoodVo good = goodService.detail(gid);
		
		if(good.getSum()<count){
			super.printJSON(new JsonMsg(5, "该商品库存不足"));
			return;
		}		
		//用户剩余积分不足
		UserAccount account = accountService.getByUserId(uid);
		Integer goodScore = count*good.getAmount();//兑换商品所需积分
		if (account == null || account.getScore() == null || account.getScore().intValue() < goodScore) {
			super.printJSON(new JsonMsg(6, "用户积分不足，兑换失败！"));
			return;
		}
		GoodBuy goodBuy=new GoodBuy();
		//记录兑换记录
		goodBuy.setCount(count);
		goodBuy.setUserId(uid);
		goodBuy.setScore(goodScore);
		goodBuy.setBuyTime(new Date());
		goodBuy.setStatus(0);
		Integer i = goodBuyService.saveOrUpdate(goodBuy);
		if (i>0) {
			//商品数量减少
			good.setSum(good.getSum()-goodBuy.getCount());
			goodService.saveOrUpdate1(good);
			//减少用户积分
			account.setScore(account.getScore()-goodScore);
			accountService.saveOrUpdate(account);
			//记录积分流水
			ScoreRecord scoreRecord = new ScoreRecord();
			scoreRecord.setUserId(uid);
			scoreRecord.setOptType("out");
			scoreRecord.setAmount(goodScore);
			scoreRecord.setWay("积分兑换");
			scoreRecord.setCreateTime(new Date());
			scoreRecord.setTerminal("PC");
			scoreRecord.setObjectId(goodBuy.getId());
			scoreRecordService.saveScoreRecord(scoreRecord);
		}
		if (i>0) {
			printJSON(new JsonMsg(-1, "兑换成功！"));
			return;
		}else{
			printJSON(new JsonMsg(7, "兑换失败！"));
			return;
		} 
	}
	
	/**
	 * 商品兑换记录 
	 * @param uid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/buy/record")
	public String buyRecord(@RequestParam("uid")Integer uid) throws Exception{
		GoodBuyVo goodBuy=new GoodBuyVo();
		goodBuy.setUserId(uid);
		PageInfo<GoodBuyVo> pageInfo = goodBuyService.listGoodBuy(goodBuy);
		printJSON(new JsonMsg(-1, "操作成功!",pageInfo.getPageList()));
		return null;
	}
	
	/**
	 * 积分流水
	 * @param uid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/score/record")
	public String scoreRecord(@RequestParam("uid") Integer uid) throws Exception{
		ScoreRecordVo vo = new ScoreRecordVo();
		vo.setUserId(uid);
		PageInfo<ScoreRecordVo> pageInfo = scoreRecordService.listScoreRecord(vo);
		printJSON(new JsonMsg(-1, "操作成功!",pageInfo.getPageList()));
		return null;
	}
}
